package com.maoye.ossp.common.web.taglib;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang3.StringUtils;

import com.maoye.ossp.sys.base.entity.SysDictDetail;
import com.maoye.ossp.sys.base.utils.SysDictUtils;
/**
 * 自定义radio标签java类
 * @author yanghao
 *
 */
public class RadioTag extends BaseTag {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String dictType;
	private String defaultChecked;
	private String id ;
	private String value;
	
	
	
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDefaultChecked() {
		return defaultChecked;
	}

	public void setDefaultChecked(String defaultChecked) {
		this.defaultChecked = defaultChecked;
	}
	
	public int doStartTag() throws JspException {
		
		StringBuilder sb = new StringBuilder();
		String paramValue = pageContext.getRequest().getAttribute(dictType)+"";
		List<Option> options = new ArrayList<Option>();
		List<SysDictDetail> sysDictDetailList = SysDictUtils.getSysDictDetailList(dictType);
		for(SysDictDetail sysDictDetail : sysDictDetailList){
			Option option = new Option();
			option.setOptionName(sysDictDetail.getValue());
			option.setOptionValue(sysDictDetail.getKey());
			options.add(option);
		}
		     
        int index = 0;
        for(Option option :options ){  
        	String  id_ = id + "_" + option.getOptionValue();  
        	sb.append(" <input type=\"radio\"  "); 
        	sb.append("name=\"");
	    	sb.append(name);
	    	sb.append("\"  ");
          	sb.append("value=\"");
	        sb.append(option.getOptionValue());
		    sb.append("\"  ");
            sb.append("id=\"");
	    	sb.append(id_);
	    	sb.append("\"  ");
	    	//value为空时，默认选中defaultChecked值，不会空是默认选中value值
	    	if(StringUtils.isNotEmpty(value)){
	    		if(value.equals(option.getOptionValue())) {
	                sb.append("checked=\"checked\"");
	            }
	    	} else if(StringUtils.isNotEmpty(defaultChecked)){
	    		if(defaultChecked.equals(option.getOptionValue())) {
	                sb.append("checked=\"checked\"");
	            }
	    	}
	    	generateAttribute(sb);
        	/*if(index == 0) {
                sb.append(" checked ");
            }*/
            if(paramValue.equals(option.getOptionValue())) {
                sb.append(" checked ");
            }
           sb.append(" /> ").append("<label for=\"").append(id_).append("\">").append(option.getOptionName()).append("</label> &nbsp;");
            sb.append("\n");
            index ++;
        }
		 try {
			 pageContext.getOut().print(sb.toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDictType() {
		return dictType;
	}
	public void setDictType(String dictType) {
		this.dictType = dictType;
	}
}
